package com.itheima.publisher.config;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import javax.annotation.PostConstruct;

@Slf4j
@RequiredArgsConstructor
public class Mqconfig2 {
    private final RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void myinit() {
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnsCallback() {
            @Override
            public void returnedMessage(ReturnedMessage returnedMessage) {
                String exchange = returnedMessage.getExchange();
                String message = new String(returnedMessage.getMessage().getBody());
                String routingKey = returnedMessage.getRoutingKey();
                log.error("消息发送失败，exchange: {}, routingKey: {}, message: {}", exchange, routingKey, message);
            }
        } );
    }
}
